package org.crazydog.converter.feet.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

import javax.swing.ButtonGroup;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import org.crazydog.converter.feet.Calculator;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class ConverterFrame extends JFrame {

	private JLabel jLInputBox;

	private JLabel jLResult;

	private JScrollPane jSOutput;

	private JComboBox jCTo;

	private JComboBox jCFrom;

	private JTextField jTResult;

	private JTextPane jTOutput;

	private JButton jBConvert;

	private JTextField jTInput;

	/**
	 * Creates an instance of our converter.
	 *
	 */
	public ConverterFrame() {
		{
			this.setSize(423, 255);
			this.setTitle("Crazy Dogs Converter v0.1");
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		}
		GridBagLayout thisLayout = new GridBagLayout();
		thisLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.1 };
		thisLayout.rowHeights = new int[] { 30, 20, 16, 7 };
		thisLayout.columnWeights = new double[] { 0.0, 0.1, 0.0, 0.1 };
		thisLayout.columnWidths = new int[] { 64, 7, 39, 7 };
		getContentPane().setLayout(thisLayout);
		getContentPane().add(
				getJTInput(),
				new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(10, 10, 5, 5), 0, 0));
		getContentPane().add(
				getJBConvert(),
				new GridBagConstraints(2, 3, 2, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(10, 5, 10, 10), 0, 0));
		getContentPane().add(
				getJLInputBox(),
				new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(10, 10, 5, 0), 0, 0));
		getContentPane().add(
				getJLResult(),
				new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(10, 5, 5, 5), 0, 0));
		getContentPane().add(
				getJTResult(),
				new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(10, 5, 5, 10), 0, 0));
		getContentPane().add(
				getJCFrom(),
				new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(5, 5, 5, 10), 0, 0));
		getContentPane().add(
				getJCTo(),
				new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(5, 5, 5, 10), 0, 0));
		getContentPane().add(
				getJSOutput(),
				new GridBagConstraints(0, 1, 2, 3, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 0, 0), 0, 0));
		getContentPane().validate();
	}

	/**
	 * Creates an instance of our converter.
	 * 
	 * @param String[]
	 */
	public static void main(String[] args) {
		{
			ConverterFrame frame = new ConverterFrame();
			frame.setVisible(true);
		}
		// TODO Auto-generated method stub

	}

	/**
	 * Gets the text in the input box.
	 * 
	 * @return String
	 */
	public JTextField getJTInput() {
		if (jTInput == null) {
			jTInput = new JTextField();
		}
		return jTInput;
	}

	private JButton getJBConvert() {
		if (jBConvert == null) {
			jBConvert = new JButton();
			jBConvert.setText("Convert");
			jBConvert.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {

					calculate();
				}

			});
		}
		return jBConvert;
	}

	/**
	 * Calculate calls the convert method and sets the output.
	 *
	 */
	public void calculate() {

		double result = Calculator.Convert(jCFrom.getSelectedIndex(), jCTo
				.getSelectedIndex(), jTInput.getText().replaceAll(" ", ""));
		if (!Calculator.isError(result)) {
			jTResult.setText(Double.toString(result));
			jTOutput.setText(jTOutput.getText() + "\n" + "From: "
					+ jCFrom.getSelectedItem() + ": "
					+ jTInput.getText().replaceAll(" ", "") + "\n" + "To: "
					+ jCTo.getSelectedItem() + ": " + jTResult.getText());
		} else {
			jTOutput.setText(jTOutput.getText() + "\n" + "ERROR - "
					+ Calculator.getError(result));
		}
	}

	private JTextPane getJTOutput() {
		if (jTOutput == null) {
			jTOutput = new JTextPane();
			jTOutput.setText("Welcome to CrazyDogs Converter");
			jTOutput.setFont(new java.awt.Font("Arial Narrow", 0, 10));
			jTOutput.setPreferredSize(new java.awt.Dimension(164, 189));
		}
		return jTOutput;
	}

	private JLabel getJLInputBox() {
		if (jLInputBox == null) {
			jLInputBox = new JLabel();
			jLInputBox.setText("Number here :");
		}
		return jLInputBox;
	}

	private JLabel getJLResult() {
		if (jLResult == null) {
			jLResult = new JLabel();
			jLResult.setText("Result :");
		}
		return jLResult;
	}

	/**
	 * Gets the text contained in the output box. Used for testing.
	 * 
	 * @return String
	 */	
	public JTextField getJTResult() {
		if (jTResult == null) {
			jTResult = new JTextField();
		}
		return jTResult;
	}

	private JComboBox getJCFrom() {
		if (jCFrom == null) {
			ComboBoxModel jCFromModel = new DefaultComboBoxModel(Calculator
					.getMetrics());
			jCFrom = new JComboBox();
			jCFrom.setModel(jCFromModel);
		}
		return jCFrom;
	}

	private JComboBox getJCTo() {
		if (jCTo == null) {
			ComboBoxModel jCToModel = new DefaultComboBoxModel(new String[] {
					"Feet", "Meter" });
			jCTo = new JComboBox();
			jCTo.setModel(jCToModel);
			jCTo.setSelectedIndex(1);
		}
		return jCTo;
	}

	private JScrollPane getJSOutput() {
		if (jSOutput == null) {
			jSOutput = new JScrollPane();
			jSOutput.setViewportView(getJTOutput());
		}
		return jSOutput;
	}

	/**
	 * Manaully sets text in the input box. Used for testing.
	 * 
	 * @param String
	 */
	public void setJTInput(String newText) {
		this.jTInput.setText(newText);
	}

}
